import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_class2/3681/accountRelated/account_controller.dart';
import 'package:toast/toast.dart';
import 'temp.dart';
import 'CreateAccountPage.dart';
import 'mid.dart';


class ccb_loginPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => ccb_loginPageState();
}

class ccb_loginPageState extends State<ccb_loginPage> {
  //手机号的控制器
  TextEditingController phoneController = new TextEditingController();

  //密码的控制器
  TextEditingController passController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.blueAccent,
      appBar: AppBar(
        title: Text('登录页面'),
      ),
      body: Column(
        children: <Widget>[
          Container(
            height: 100,
            width: 100,
            child: Image.asset('images/ccb.png',width: 100,height: 100),
            alignment: Alignment.center,
          ),

          Text('欢迎登录CCTB',style: TextStyle(
            fontSize: 35.0,
          )),
          TextField(
            controller: phoneController,
            keyboardType: TextInputType.url,
            inputFormatters: [
              LengthLimitingTextInputFormatter(8),
            ],
            decoration: InputDecoration( //decoration参数接收一个InputDecoration类型的值
              contentPadding: EdgeInsets.all(10.0),//内间距
              icon: Icon(Icons.supervised_user_circle),
              labelText: '请输入你的用户名或手机号',
            ),
            autofocus: false,
          ),
          TextField(
              controller: passController,
              keyboardType: TextInputType.number,
              decoration: InputDecoration(
                contentPadding: EdgeInsets.all(10.0),
                icon: Icon(Icons.lock),
                labelText: '请输入密码',
              ),
              obscureText: true),
          RaisedButton(
            color: Colors.blue,
            textColor: Colors.white,
            onPressed: _login,
            child: Text('登录'),
          ),
          RaisedButton(
            color: Colors.blue,
            textColor: Colors.white,
            onPressed: _login_,
            child: Text('注册'),
          ),
        ],
      ),
    );
  }

  void _login() async{
    List accounts;
    var accountDatabaseHelper = new AccountDatabaseHelper();

    print({'userName': phoneController.text, 'password': passController.text});
    print('=== getccount(username) ===');
    Account account_test = await accountDatabaseHelper.getAccount(phoneController.text);
    if(account_test != null ){
      if(passController.text == account_test.password){
        Navigator.push(context, MaterialPageRoute(builder: (context)=>TkPage()));
        String name= passController.text;
        showDialog(
            context: context,
            builder: (context) =>
                AlertDialog(
                  title: Text('$name,欢迎您！'),
                ));

      }else{
        showDialog(
            context: context,
            builder: (context) =>
                AlertDialog(
                  title: Text('密码有误'),
                ));
      }
    }else{
      showDialog(
          context: context,
          builder: (context) =>
              AlertDialog(
                title: Text('用户名有误'),
              ));
    }
    // 测试数据库用-----------↓

    print('=== getAllAccounts() ===');
    accounts = await accountDatabaseHelper.getAllAccounts();
    accounts.forEach((account) => print(account));
    print('=== getccount(123456) ===');
    Account account = await accountDatabaseHelper.getAccount("123456");
    print(account);
    print('=== getccount(12345678) ===');
    Account accoun = await accountDatabaseHelper.getAccount("ccb");
    print(accoun.id + " " +accoun.password);
    //-----------------↑

     if (passController.text.length == 0) {
      showDialog(
          context: context,
          builder: (context) => AlertDialog(
            title: Text('请填写密码'),
          ));
    } else {
      Navigator.push(context, MaterialPageRoute(builder: (context)=>TkPage()));
      phoneController.clear();
    }
  }


  void _login_(){

    Navigator.push(context, MaterialPageRoute(builder: (context)=>CAPage()));
  }
  void onTextClear() {
    setState(() {
      phoneController.clear();
      passController.clear();
    });
  }
}

